package Foundation;

import java.util.GregorianCalendar;
import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


import DomainPackage.SatisElemani;
import TechnicalServices.*;
public class SatisElemaniMapper implements IMapper {

	@Override
	public Object get(int ID) {
		SatisElemani satiselemani = new SatisElemani(-1);
		ResultSet rs = DataConnector.getInstance().query("select * from satiselemani where elemanid = " + ID + "");
		
		if (rs != null){
            
            try {
                    rs.next();
                    
                    
                    satiselemani.setElemanID(Integer.parseInt(rs.getString(1)));
                    satiselemani.setAdi(rs.getString(2));
                    satiselemani.setSoyadi(rs.getString(3));
                    satiselemani.setDogumTarihi(new GregorianCalendar(Integer.parseInt(rs.getString(4).split("-")[0]), 
                    							Integer.parseInt(rs.getString(4).split("-")[1]), 
                    							Integer.parseInt(rs.getString(4).split("-")[2])));
                             return satiselemani;
                   
            } catch (NumberFormatException e) {
                    System.out.println("SatisElemaniMapper: number format problem");
                    e.printStackTrace();
            } catch (SQLException e) {
                    System.out.println("SatisElemaniMapper: sql problem");
                    e.printStackTrace();
            }
    }
    else
            System.out.println("no satiselemani with given ID");
    return null;
	}

	@Override
	public int put(Object object) {
		SatisElemani satiselemani=(SatisElemani) object;
			String dt = "";
			GregorianCalendar dta = satiselemani.getDogumTarihi();
			dt += String.valueOf(dta.get(GregorianCalendar.YEAR)) + "-";
			dt += String.valueOf(dta.get(GregorianCalendar.MONTH)) + '-';
			dt += String.valueOf(dta.get(GregorianCalendar.DAY_OF_MONTH));
            if(!DataConnector.getInstance().update("INSERT INTO satiselemani "+
                    "(adi, soyadi, elemanid, dogumtarihi) " +
                    "values ('"+satiselemani.getAdi()+"', '"+
                    satiselemani.getSoyadi()+"', '"+
                    satiselemani.getElemanID()+"', '"+
                    dt+"')")){
            	System.out.println("error");
            }
			
         return 0;

	}

	@Override
	public List<Object> getAll() {
		return null;
	}

}
